package com.bjsxt.service.impl;

import com.bjsxt.mapper.RoleMapper;
import com.bjsxt.mapper.RoleMenuMapper;
import com.bjsxt.pojo.PageA;
import com.bjsxt.pojo.Role;
import com.bjsxt.service.RoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    /*查询所有角色信息*/
    @Override
    public PageA<Role> findMoreRole(Integer page, Integer rows) {
        Page pg = PageHelper.startPage(page, rows);
        List<Role> list = roleMapper.findMoer();
        PageA<Role> li=new PageA<Role>(list,pg.getTotal());
        return li;
    }
    //添加角色信息
    @Override
    public Integer savaRole(Integer[] mids,Role role) {
        Integer a=0;
        try {
            //添加角色的实现
            roleMapper.insert(role);
            for (Integer mid:mids){
                //添加角色菜单表的实现
                roleMenuMapper.insert(role.getRid(), mid);
            }
            a=1;
        } catch (Exception e) {
         throw  e;
        }
        return a;
    }
  //查询指定角色下的菜单编号mid
    @Override
    public List<Integer> findMoreMID(Integer rid) {
        return roleMenuMapper.selectMore(rid);
    }
  //修改角色的信息
    @Override
    public Integer updateRole(Role role,Integer[] mids) {
        Integer a=0;
        try {
            //修改角色的信息
            roleMapper.update(role);
            //删除角色下的菜单
            roleMenuMapper.delete(role.getRid());
            //添加角色下的信息
            for (Integer mid:mids){
                roleMenuMapper.insert(role.getRid(),mid);
            }
            a=1;
        } catch (Exception e) {
            throw  e;
        }

        return a;
    }
   //删除角色的信息
    @Override
    public Integer deleteRole(Integer rid) {
        return roleMapper.deleteRole(rid);
    }
   //查询所有的角色
    @Override
    public List<Role> findAllRole() {
        return   roleMapper.findMoer();
    }
}
